﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MovieRecorder.CApMRCMovieRecorderSceneManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Options;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.MovieRecorder;

public class CApMRCMovieRecorderSceneManager
{
  private CApMRCMovieRecorderSceneSettings _originalSceneSettings = new CApMRCMovieRecorderSceneSettings();
  private Dictionary<ITxDisplayableObject, TxDisplayableObjectVisibility> _originalOperationsVisibility;
  private Dictionary<ITxDisplayableObject, TxDisplayableObjectVisibility> _original2DObjectsVisibility;
  private Dictionary<ITxDisplayableObject, TxDisplayableObjectVisibility> _originalObjectsVisibility;
  private TxObjectList _objectsForDisplaying;
  private TxObjectList _objectsForBlanking;
  private bool _isWorkingFrameSettingSaved;
  private bool _isNavigationCubeSettingSaved;
  private bool _isEnablePartCullingSettingSaved;
  private bool _isShowOperationsSettingSaved;
  private bool _isShow2DObjectsSettingSaved;
  private bool _isGVToolbarSettingSaved;
  private bool _isSimulationSliderSettingSaved;
  private bool _areOperationsDisplayed = true;
  private bool _are2DObjectsDisplayed = true;
  private TxTypeFilter _operationsFilter;
  private TxTypeFilter _2dObjectsFilter;

  public CApMRCMovieRecorderSceneManager()
  {
    this.CreateOperationsFilter();
    this.Create2DObjectsFilter();
  }

  private void CreateOperationsFilter()
  {
    this._operationsFilter = new TxTypeFilter(typeof (ITxOperation));
    this._operationsFilter.AddIncludedType(typeof (ITxObjectCollection));
  }

  private void Create2DObjectsFilter()
  {
    this._2dObjectsFilter = new TxTypeFilter(typeof (ITx1Dimensional));
    this._2dObjectsFilter.AddIncludedType(typeof (ITx1DimensionalGeometry));
    this._2dObjectsFilter.AddIncludedType(typeof (TxFrame));
  }

  private void SaveOperationsVisibility()
  {
    this._originalOperationsVisibility = new Dictionary<ITxDisplayableObject, TxDisplayableObjectVisibility>();
    foreach (ITxObject allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.OperationRoot.GetAllDescendants((ITxTypeFilter) this._operationsFilter))
    {
      if (allDescendant is ITxDisplayableObject key)
        this._originalOperationsVisibility.Add(key, key.Visibility);
    }
  }

  private void Save2DObjectsVisibility()
  {
    this._original2DObjectsVisibility = new Dictionary<ITxDisplayableObject, TxDisplayableObjectVisibility>();
    foreach (ITxObject allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) this._2dObjectsFilter))
    {
      if (allDescendant is ITxDisplayableObject key)
        this._original2DObjectsVisibility.Add(key, key.Visibility);
    }
  }

  private bool RestoreOperationsVisibility()
  {
    this._originalObjectsVisibility = this._originalOperationsVisibility;
    return this.RestoreCollectionVisibility((ITxObjectCollection) TxApplication.ActiveDocument.OperationRoot, this._operationsFilter);
  }

  private bool Restore2DimensionalObjectsVisibility()
  {
    this._originalObjectsVisibility = this._original2DObjectsVisibility;
    return this.RestoreCollectionVisibility((ITxObjectCollection) TxApplication.ActiveDocument.PhysicalRoot, this._2dObjectsFilter);
  }

  private void SetSceneVisibility(
    bool showWorkingFrame,
    bool showNavigationCube,
    bool showNavigationFrame,
    bool enablePartCulling,
    bool showOperations,
    bool show2DObjects,
    bool showGVToolbar,
    bool showSimSlider)
  {
    bool flag = false;
    if (showWorkingFrame != TxOptionsEx.ShowWorkingFrame)
    {
      TxOptionsEx.ShowWorkingFrame = showWorkingFrame;
      flag = true;
    }
    this.ShowOptionsRequiredOptionsUpdate(showNavigationCube, showNavigationFrame, showSimSlider, showGVToolbar);
    if (enablePartCulling != TxOptionsEx.EnablePartCulling)
    {
      TxOptionsEx.EnablePartCulling = enablePartCulling;
      TxGraphicViewerEx.RequireRefreshSynchronization();
      flag = true;
    }
    if (showOperations != this._areOperationsDisplayed)
    {
      if (showOperations)
        flag |= this.RestoreOperationsVisibility();
      else
        flag |= this.HideOperations();
      this._areOperationsDisplayed = showOperations;
    }
    if (show2DObjects != this._are2DObjectsDisplayed)
    {
      if (show2DObjects)
        flag |= this.Restore2DimensionalObjectsVisibility();
      else
        flag |= this.Hide2DimensionalObjects();
      this._are2DObjectsDisplayed = show2DObjects;
    }
    if (!flag)
      return;
    TxApplication.RefreshDisplay();
  }

  private void ShowOptionsRequiredOptionsUpdate(
    bool showNavigationCube,
    bool showNavigationFrame,
    bool showSimSlider,
    bool showGVToolbar)
  {
    bool flag = false;
    if (showNavigationCube != TxOptionsEx.ShouldShowNavigationCube)
    {
      TxOptionsEx.ShouldShowNavigationCube = showNavigationCube;
      flag = true;
    }
    if (showNavigationFrame != TxOptionsEx.ShouldShowNavigationFrame)
    {
      TxOptionsEx.ShouldShowNavigationFrame = showNavigationFrame;
      flag = true;
    }
    if (showSimSlider != TxOptionsEx.ShouldShowSliderOnSimulation)
    {
      TxOptionsEx.ShouldShowSliderOnSimulation = showSimSlider;
      flag = true;
    }
    if (showGVToolbar != TxApplication.Options.GraphicViewer.EnableToolbar)
    {
      TxApplication.Options.GraphicViewer.EnableToolbar = showGVToolbar;
      flag = true;
    }
    if (!flag)
      return;
    TxOptionsEx.SaveSettingsData();
    TxOptionsEx.FireOnSettingsChangedEvent();
  }

  private static bool HideVisibleObjects(ITxObjectCollection root, TxTypeFilter objType)
  {
    bool flag = false;
    TxObjectList allDescendants = root.GetAllDescendants((ITxTypeFilter) objType);
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) allDescendants)
    {
      if (itxObject is ITxDisplayableObject displayableObject && (displayableObject.Visibility == null || displayableObject.Visibility == 1))
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) displayableObject);
    }
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      TxGraphicsUtilitiesEx.BlankObjects(txObjectList, false);
      flag = true;
    }
    return flag;
  }

  private bool RestoreCollectionVisibility(ITxObjectCollection collection, TxTypeFilter objFilter)
  {
    bool flag = false;
    this._objectsForDisplaying = new TxObjectList();
    this._objectsForBlanking = new TxObjectList();
    this.GetObjectsVisibility(collection, objFilter);
    if (((Collection<ITxObject>) this._objectsForDisplaying).Count > 0)
    {
      TxGraphicsUtilitiesEx.DisplayObjects(this._objectsForDisplaying, false);
      flag = true;
    }
    if (((Collection<ITxObject>) this._objectsForBlanking).Count > 0)
    {
      TxGraphicsUtilitiesEx.BlankObjects(this._objectsForBlanking, false);
      flag = true;
    }
    return flag;
  }

  private void GetObjectsVisibility(ITxObjectCollection collection, TxTypeFilter objFilter)
  {
    TxTypeFilter txTypeFilter = new TxTypeFilter(typeof (ITxObjectCollection));
    foreach (ITxObjectCollection collection1 in ((IEnumerable<ITxObject>) collection.GetDirectDescendants((ITxTypeFilter) txTypeFilter)).ToList<ITxObject>())
    {
      if (((ITxObject) collection1).IsValid())
      {
        this.GetObjectVisibility(collection1 as ITxDisplayableObject);
        this.GetObjectsVisibility(collection1, objFilter);
      }
    }
    objFilter.RemoveIncludedType(typeof (ITxObjectCollection));
    foreach (ITxObject itxObject in ((IEnumerable<ITxObject>) collection.GetDirectDescendants((ITxTypeFilter) objFilter)).ToList<ITxObject>())
    {
      if (itxObject.IsValid())
        this.GetObjectVisibility(itxObject as ITxDisplayableObject);
    }
  }

  private void GetObjectVisibility(ITxDisplayableObject obj)
  {
    TxDisplayableObjectVisibility objectVisibility;
    if (obj == null || !this._originalObjectsVisibility.TryGetValue(obj, out objectVisibility))
      return;
    if (objectVisibility == null && obj.Visibility != null || objectVisibility == 1 && obj.Visibility != 1)
    {
      ((Collection<ITxObject>) this._objectsForDisplaying).Add((ITxObject) obj);
    }
    else
    {
      if (objectVisibility != 2 || obj.Visibility == 2)
        return;
      ((Collection<ITxObject>) this._objectsForBlanking).Add((ITxObject) obj);
    }
  }

  private bool HideOperations()
  {
    return CApMRCMovieRecorderSceneManager.HideVisibleObjects((ITxObjectCollection) TxApplication.ActiveDocument.OperationRoot, this._operationsFilter);
  }

  private bool Hide2DimensionalObjects()
  {
    return CApMRCMovieRecorderSceneManager.HideVisibleObjects((ITxObjectCollection) TxApplication.ActiveDocument.PhysicalRoot, this._2dObjectsFilter);
  }

  public void SetSceneVisibility(CApMRCMovieRecorderSceneSettings sceneSettings)
  {
    bool showWorkingFrame = false;
    if (sceneSettings.ShowWorkingFrame)
      showWorkingFrame = this._originalSceneSettings.ShowWorkingFrame;
    bool showNavigationCube = false;
    if (sceneSettings.ShowNavigationCube)
      showNavigationCube = this._originalSceneSettings.ShowNavigationCube;
    bool showNavigationFrame = false;
    if (sceneSettings.ShowNavigationFrame)
      showNavigationFrame = this._originalSceneSettings.ShowNavigationFrame;
    bool enablePartCulling = true;
    if (!sceneSettings.EnablePartCulling)
      enablePartCulling = this._originalSceneSettings.EnablePartCulling;
    bool showOperations = false;
    if (sceneSettings.ShowOperations)
      showOperations = this._originalSceneSettings.ShowOperations;
    bool show2DObjects = false;
    if (sceneSettings.Show2DObjects)
      show2DObjects = this._originalSceneSettings.Show2DObjects;
    bool showGVToolbar = false;
    if (sceneSettings.ShowGraphicViewerToolbar || !sceneSettings.HideGraphicViewerControlsAllowed)
      showGVToolbar = this._originalSceneSettings.ShowGraphicViewerToolbar;
    bool showSimSlider = false;
    if (sceneSettings.ShowSimulationSlider || !sceneSettings.HideGraphicViewerControlsAllowed)
      showSimSlider = this._originalSceneSettings.ShowSimulationSlider;
    this.SetSceneVisibility(showWorkingFrame, showNavigationCube, showNavigationFrame, enablePartCulling, showOperations, show2DObjects, showGVToolbar, showSimSlider);
  }

  public void RestoreOriginalScene() => this.SetSceneVisibility(this._originalSceneSettings);

  public void SaveSceneSettings(CApMRCMovieRecorderSceneSettings sceneSettings)
  {
    if (!this._isWorkingFrameSettingSaved)
    {
      this._originalSceneSettings.ShowWorkingFrame = TxOptionsEx.ShowWorkingFrame;
      this._isWorkingFrameSettingSaved = true;
    }
    if (!this._isNavigationCubeSettingSaved)
    {
      this._originalSceneSettings.ShowNavigationCube = TxOptionsEx.ShouldShowNavigationCube;
      this._originalSceneSettings.ShowNavigationFrame = TxOptionsEx.ShouldShowNavigationFrame;
      this._isNavigationCubeSettingSaved = true;
    }
    if (!this._isEnablePartCullingSettingSaved)
    {
      this._originalSceneSettings.EnablePartCulling = TxOptionsEx.EnablePartCulling;
      this._isEnablePartCullingSettingSaved = true;
    }
    if (!sceneSettings.ShowOperations && !this._isShowOperationsSettingSaved)
    {
      this.SaveOperationsVisibility();
      this._isShowOperationsSettingSaved = true;
    }
    if (!sceneSettings.Show2DObjects && !this._isShow2DObjectsSettingSaved)
    {
      this.Save2DObjectsVisibility();
      this._isShow2DObjectsSettingSaved = true;
    }
    if (!this._isGVToolbarSettingSaved)
    {
      this._originalSceneSettings.ShowGraphicViewerToolbar = TxApplication.Options.GraphicViewer.EnableToolbar;
      this._isGVToolbarSettingSaved = true;
    }
    if (this._isSimulationSliderSettingSaved)
      return;
    this._originalSceneSettings.ShowSimulationSlider = TxOptionsEx.ShouldShowSliderOnSimulation;
    this._isSimulationSliderSettingSaved = true;
  }
}
